package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class UnitInfoBeanParser {
	public static List<UnitInfoBean> parseXML(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		UnitInfoBean unitInfo = null;
		List<UnitInfoBean> list = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<UnitInfoBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						unitInfo = new UnitInfoBean();
					} else if (null != unitInfo) {
						if ("UNIT".equals(parser.getName())) {
							unitInfo.setUnit(parser.nextText());
						} else if ("UNITNAME".equals(parser.getName())) {
							unitInfo.setUnitname(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						list.add(unitInfo);
						unitInfo = null;
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}

	public static List<UnitInfoBean> parseXMLForVisit(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		UnitInfoBean unitInfo = null;
		List<UnitInfoBean> list = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<UnitInfoBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("comb_unit".equals(parser.getName())) {
						unitInfo = new UnitInfoBean();
					} else if (null != unitInfo) {
						if ("UNIT".equals(parser.getName())) {
							unitInfo.setUnit(parser.nextText());
						} else if ("UNITNAME".equals(parser.getName())) {
							unitInfo.setUnitname(parser.nextText());
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("comb_unit".equals(parser.getName())) {
						list.add(unitInfo);
						unitInfo = null;
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}
}
